package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.common.UserHolder;
import com.itheima.ydd.entity.*;
import com.itheima.ydd.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("ydd_cate")
public class CateController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private RoleMenuService roleMenuService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private CateService cateService;
    //页面查询
    @GetMapping("list")
    public ResultData list(String catename,Long page,Long limit){
        log.info("查询条件:{},page:{],limit:[}",catename,page,limit);
        //创建页面构造器
        Page<Cate> catePage=new Page<>(page,limit);
        //创建条件构造器
        LambdaQueryWrapper<Cate> lqw=new LambdaQueryWrapper<>();
        if (!("".equals(catename))) {
            lqw.like(Cate::getCatename, catename);
        }
        lqw.orderByDesc(Cate::getSort_num);
        cateService.page(catePage,lqw);
        return ResultData.ok("查询成功",PageResult.init(catePage.getTotal(),page,limit,catePage.getRecords()));
    }
    //删除
    @GetMapping("del")
    public ResultData delete(Integer id){
        log.info("删除的id为:{}",id);
        Cate cate = cateService.getById(id);
        //判断是否为系统默认数据
        if ("1".equals(cate.getDefault_data())){
            return ResultData.error("系统默认数据，无法删除");
        }
       /* LambdaQueryWrapper<Role> roleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //获取登录人员信息，把test类型转化为string，去掉[]和""
        String userId = UserHolder.getUserId();
        User user2 = userService.getById(userId);
        String strip = StringUtils.strip(user2.getRole_id(), "[]");
        String[] split = strip.split(",");
        for (int i = 0; i < split.length; i++) {
            split[i]=StringUtils.replace(split[i], "\"", "");
        }
        //String replace = StringUtils.replace(strip, "\"", "");
        //String[] split = replace.split(",");
        roleLambdaQueryWrapper.in(Role::getId,split);
        List<Role> list1 = roleService.list(roleLambdaQueryWrapper);
        List<String> collect = list1.stream().map((item) -> {
            return item.getId();
        }).collect(Collectors.toList());
        //根据权限id去查询是否有分类管理权限
        LambdaQueryWrapper<RoleMenu> roleMenuLambdaQueryWrapper=new LambdaQueryWrapper<>();
        roleMenuLambdaQueryWrapper.in(RoleMenu::getRoleId,collect);
        List<RoleMenu> list = roleMenuService.list(roleMenuLambdaQueryWrapper);
        for (RoleMenu roleMenu : list) {
            String menuId = roleMenu.getMenuId();
            Menu menu = menuService.getById(menuId);
            //一级权限
            if ("内容管理".equals(menu.getTitle())) {
                LambdaQueryWrapper<Menu> menuLambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                menuLambdaQueryWrapper1.eq(Menu::getPid, menuId);
                List<Menu> list3 = menuService.list(menuLambdaQueryWrapper1);
                for (Menu Menu2 : list3) {
                    //二级权限
                    if ("分类管理".equals(Menu2.getTitle())) {
                        LambdaQueryWrapper<Menu> menuLambdaQueryWrapper = new LambdaQueryWrapper<>();
                        menuLambdaQueryWrapper.eq(Menu::getPid, Menu2.getId());
                        List<Menu> list2 = menuService.list(menuLambdaQueryWrapper);
                        for (Menu Menu1 : list2) {
                            //Menu menu1 = menuService.getById(Menu1.getId());
                            //三级权限
                            if ("删除".equals(Menu1.getTitle())) {
                                //根据用户id删除分类数据
                                cateService.removeById(id);
                                return ResultData.ok("删除成功", "删除成功");
                            }
                        }
                    }
                }
            }
        }*/
        if (roleService.RoleUtils("内容管理","分类管理","删除")){
            cateService.removeById(id);
            return ResultData.ok("删除成功", "删除成功");
        }
        //否则没有权限删除
        return ResultData.error("删除失败，该用户没有权限");
    }
    //查询单个
    @GetMapping("rowInfo")
    public ResultData getById(Integer id){
        log.info("查询的id为:{}",id);
        Cate cate = cateService.getById(id);
        return ResultData.ok("查询成功",cate);
    }
    //更改
    @PostMapping("update")
    public ResultData update(@RequestBody Cate cate){
        log.info("更新的数据为:{}",cate);
       /* LambdaQueryWrapper<Role> roleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //获取登录人员信息，把test类型转化为string，去掉[]和""
        String userId = UserHolder.getUserId();
        User user2 = userService.getById(userId);
        String strip = StringUtils.strip(user2.getRole_id(), "[]");
        String[] split = strip.split(",");
        for (int i = 0; i < split.length; i++) {
            split[i]=StringUtils.replace(split[i], "\"", "");
        }
        //String replace = StringUtils.replace(strip, "\"", "");
        //String[] split = replace.split(",");
        roleLambdaQueryWrapper.in(Role::getId,split);
        List<Role> list1 = roleService.list(roleLambdaQueryWrapper);
        List<String> collect = list1.stream().map((item) -> {
            return item.getId();
        }).collect(Collectors.toList());
        //根据权限id去查询是否有分类管理权限
        LambdaQueryWrapper<RoleMenu> roleMenuLambdaQueryWrapper=new LambdaQueryWrapper<>();
        roleMenuLambdaQueryWrapper.in(RoleMenu::getRoleId,collect);
        List<RoleMenu> list = roleMenuService.list(roleMenuLambdaQueryWrapper);
        for (RoleMenu roleMenu : list) {
            String menuId = roleMenu.getMenuId();
            Menu menu = menuService.getById(menuId);
            //一级权限
            if ("内容管理".equals(menu.getTitle())) {
                LambdaQueryWrapper<Menu> menuLambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                menuLambdaQueryWrapper1.eq(Menu::getPid, menuId);
                List<Menu> list3 = menuService.list(menuLambdaQueryWrapper1);
                for (Menu Menu2 : list3) {
                    //二级权限
                    if ("分类管理".equals(Menu2.getTitle())) {
                        LambdaQueryWrapper<Menu> menuLambdaQueryWrapper = new LambdaQueryWrapper<>();
                        menuLambdaQueryWrapper.eq(Menu::getPid, Menu2.getId());
                        List<Menu> list2 = menuService.list(menuLambdaQueryWrapper);
                        for (Menu Menu1 : list2) {
                            //Menu menu1 = menuService.getById(Menu1.getId());
                            //三级权限
                            if ("更新".equals(Menu1.getTitle())) {
                                //根据cate更新
                                cateService.updateById(cate);
                                return ResultData.ok("更新成功", "更新成功");
                            }
                        }
                    }
                }
            }
        }*/
        if (roleService.RoleUtils("内容管理","分类管理","更新")){
            cateService.updateById(cate);
            return ResultData.ok("更新成功", "更新成功");
        }
        //否则没有权限更新
        return ResultData.error("更新失败，该用户没有权限!");
    }
    //添加
    @PostMapping("add")
    public ResultData add(@RequestBody Cate cate){
        log.info("添加的数据为:{]",cate);
        /*LambdaQueryWrapper<Role> roleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //获取登录人员信息，把test类型转化为string，去掉[]和""
        String userId = UserHolder.getUserId();
        User user2 = userService.getById(userId);
        String strip = StringUtils.strip(user2.getRole_id(), "[]");
        String[] split = strip.split(",");
        for (int i = 0; i < split.length; i++) {
            split[i]=StringUtils.replace(split[i], "\"", "");
        }
        //String replace = StringUtils.replace(strip, "\"", "");
        //String[] split = replace.split(",");
        roleLambdaQueryWrapper.in(Role::getId,split);
        List<Role> list1 = roleService.list(roleLambdaQueryWrapper);
        List<String> collect = list1.stream().map((item) -> {
            return item.getId();
        }).collect(Collectors.toList());
        //根据权限id去查询是否有分类管理权限
        LambdaQueryWrapper<RoleMenu> roleMenuLambdaQueryWrapper=new LambdaQueryWrapper<>();
        roleMenuLambdaQueryWrapper.in(RoleMenu::getRoleId,collect);
        List<RoleMenu> list = roleMenuService.list(roleMenuLambdaQueryWrapper);
        for (RoleMenu roleMenu : list) {
            String menuId = roleMenu.getMenuId();
            Menu menu = menuService.getById(menuId);
            //一级权限
            if ("内容管理".equals(menu.getTitle())) {
                LambdaQueryWrapper<Menu> menuLambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                menuLambdaQueryWrapper1.eq(Menu::getPid, menuId);
                List<Menu> list3 = menuService.list(menuLambdaQueryWrapper1);
                for (Menu Menu2 : list3) {
                    //二级权限
                    if ("分类管理".equals(Menu2.getTitle())) {
                        LambdaQueryWrapper<Menu> menuLambdaQueryWrapper = new LambdaQueryWrapper<>();
                        menuLambdaQueryWrapper.eq(Menu::getPid, Menu2.getId());
                        List<Menu> list2 = menuService.list(menuLambdaQueryWrapper);
                        for (Menu Menu1 : list2) {
                            //Menu menu1 = menuService.getById(Menu1.getId());
                            //三级权限
                            if ("增加".equals(Menu1.getTitle())) {
                                //根据cate增加
                                cateService.save(cate);
                                return ResultData.ok("添加成功", "添加成功");
                            }
                        }
                    }
                }
            }
        }*/
        if (roleService.RoleUtils("内容管理","分类管理","增加")){
            cateService.save(cate);
            return ResultData.ok("添加成功", "添加成功");
        }
        //否则没有权限更新
        return ResultData.error("添加失败，该用户没有权限!");
    }
    //获取所以分类
    @GetMapping("all")
    public ResultData all(){
        List<Cate> list = cateService.list();
        return ResultData.ok("查询成功",list);
    }
}
